import request from "../request";
import type { Response } from "../types";

export type MessageType = {
  id: number;
  message: string;
  avatar: string;
  name: string;
  lastMessageTime: string;
  messageSendType: boolean;
  noMessageNum: number;
  type: "friend" | "group";
  messageType: "data" | "img" | "video" | "voice";
};

export type BaseMessage<T extends object | null> = {
  createdAt: string;
  messageSendType: 0 | 1;
  messageType: string;
  content: string;
  messageId?: number;
  messageStatus?: number;
} & T;

export type FriendMessage = {
  avatar: string;
  id: number;
  name: string;
};
const getMessageList: Response<
  {
    userId: number;
  },
  MessageType[]
> = params => {
  return request.get("/message/getmessagelist", {
    params: params
  });
};
const searchMessage: Response<
  {
    userId: number;
    keyword: string;
  },
  MessageType[]
> = params => {
  return request.post("/message/search", params);
};
const setMessageRead: Response<
  {
    messageId: number;
    type: "group" | "friend";
    userId: number;
  },
  Record<string, never>
> = params => {
  return request.post("/message/read", params);
};

const GetHistoryMessage: Response<
  {
    messageId: number;
    messageSize: number;
    type: "group" | "friend";
    startId?: number;
    userId: number;
  },
  {
    list: (BaseMessage<FriendMessage> | BaseMessage<null>)[];
    total: number;
  }
> = params => {
  return request.post("/message/historymessage", params);
};

const GetFullMessageHistory: Response<
  {
    messageId: number;
    type: "group" | "friend";
    userId: number;
    messageSize?: number;
    pageNum?: number;
  },
  {
    list: (BaseMessage<FriendMessage> | BaseMessage<null>)[];
    total: number;
    hasMore: boolean;
  }
> = params => {
  return request.post("/message/fullhistory", params);
};

const getGroupDetil: Response<
  {
    messageId: number;
    userId: number;
  },
  Record<string, never>
> = params => {
  return request.post("/group/getgroup", params);
};
const changeGroupCommendName: Response<
  {
    userCommendName?: string;
    groupName?: string;
    type: string;
    userId: number;
    groupId: number;
  },
  Record<string, never>
> = params => {
  return request.post("/group/changecommendname", params);
};
const delectGroupUser: Response<
  {
    userId: number;
    groupId: number;
  },
  Record<string, never>
> = params => {
  return request.post("/group/unentryuser", params);
};

const changeGroupName: Response<
  {
    groupId: number;
    name: string;
  },
  Record<string, never>
> = params => {
  return request.post("/group/resetGroupName", params);
};
const SendMessage: Response<
  {
    id: number;
    userId: number;
    content: string;
    type: string;
    messageType?: string;
    messageSendType?: number;
  },
  Record<string, never>
> = params => {
  return request.post("/message/sendmessage", params);
};

const RecallMessage: Response<
  {
    messageId: string | number;
    type: "group" | "friend";
    userId: number;
  },
  Record<string, never>
> = params => {
  return request.post("/message/recall", params);
};

export {
  getMessageList,
  searchMessage,
  setMessageRead,
  GetHistoryMessage,
  GetFullMessageHistory,
  getGroupDetil,
  changeGroupCommendName,
  delectGroupUser,
  changeGroupName,
  SendMessage,
  RecallMessage
};
